/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__frhash_stat_l7_add.c                            */
/*  NOTE      =                                                    */
/*  DATE      = 2015/02/03 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "frcomm.h"
#include "frmem.h"
#include "frhash2.h"
#include "frsutl.h"
#include "frstat_ct.h"
#include "apmtest_cunit.h"

static int __suite_init(void)
{
	return 0;
}

static int __suite_cleanup(void)
{
	return 0;
}

typedef struct __st_ct_conn_stat_l7 {
    int i_l7_proto;
    unsigned int ui_all_conn_count;
    unsigned int ui_dead_conn_count;

} Frct_conn_stat_l7;

void __frhash_stat_l7_add(Frhash_header *p_st_hash_l7, int i_l7_proto, int i_dead_conn);

void __hash_node_conn_stat_free(Frlist_node *p_st_node);

void case1(void)
{
    Frhash_header *p_st_hash_l7 = NULL;
    int i_l7_proto = 1433;
    int i_dead_conn = 1;

    __frhash_stat_l7_add(p_st_hash_l7, i_l7_proto, i_dead_conn);

    CU_ASSERT_PTR_NULL(p_st_hash_l7);
    return;

}


void case2(void)
{
    Frhash_header *p_st_hash_l7 = NULL;
    int i_l7_proto = 1433;
    int i_dead_conn = 1;
    Frlist_node *p_st_node = NULL;
    Frct_conn_stat_l7 *p_st_conn_stat_l7 = NULL;

    p_st_hash_l7 = frhash2_create2(5);

    __frhash_stat_l7_add(p_st_hash_l7, i_l7_proto, i_dead_conn);

    p_st_node = frhash2_getLong(p_st_hash_l7, i_l7_proto);

    p_st_conn_stat_l7 = (Frct_conn_stat_l7 *)(p_st_node->data);

    CU_ASSERT_EQUAL(p_st_conn_stat_l7->ui_all_conn_count,1);
    CU_ASSERT_EQUAL(p_st_conn_stat_l7->ui_dead_conn_count,1);
    CU_ASSERT_EQUAL(p_st_conn_stat_l7->i_l7_proto,1433);

    frhash2_free2(p_st_hash_l7,__hash_node_conn_stat_free);
    return;

}

void case3(void)
{
    Frhash_header *p_st_hash_l7 = NULL;
    int i_l7_proto = 1433;
    int i_dead_conn = 1;
    Frlist_node *p_st_node = NULL;
    Frct_conn_stat_l7 *p_st_conn_stat_l7 = NULL;

    p_st_hash_l7 = frhash2_create2(5);

    __frhash_stat_l7_add(p_st_hash_l7, i_l7_proto, i_dead_conn);

    i_dead_conn = 0;
    __frhash_stat_l7_add(p_st_hash_l7, i_l7_proto, i_dead_conn);

    p_st_node = frhash2_getLong(p_st_hash_l7, i_l7_proto);

    p_st_conn_stat_l7 = (Frct_conn_stat_l7 *)(p_st_node->data);

    CU_ASSERT_EQUAL(p_st_conn_stat_l7->ui_all_conn_count,2);
    CU_ASSERT_EQUAL(p_st_conn_stat_l7->ui_dead_conn_count,1);
    CU_ASSERT_EQUAL(p_st_conn_stat_l7->i_l7_proto,1433);

    frhash2_free2(p_st_hash_l7,__hash_node_conn_stat_free);
    return;

}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("t__frhash_stat_l7_add 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);
   	CU_add_test(pSuite, "case2", case2);
   	CU_add_test(pSuite, "case3", case3);

    return 0;

}

int main(int argc, char **argv)
{
    fr_slab_init(52428800, "test");
    run_cunit(add_suite);
    fr_slab_destroy();

    return 0;
}